package com.chub.study.util.jsr310;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author : chub
 * @version : V1.0.0
 * @fileName : JSONTester.java
 * @description : TODO
 * @createTime : 2021-09-23 22:23
 */
@Slf4j
public class JSR310Tester {

    public static void main(String[] args) throws ParseException, IOException {

        log.info("下面开始调用序列化和反序列化测试---------->\n");
        Person p2 = new Person();
        p2.setAge("45");
        p2.setName("张三");
        p2.setBirthDay(LocalDateTime.of(1987,4,5,12,50, 12));
        p2.setBirthDay1(LocalDateTime.of(1988,4,5,12,50, 12));
        p2.setBirthDay2(LocalDate.of(1989,4,5));
        p2.setBirthDay3(new java.sql.Date(System.currentTimeMillis()));
        p2.setBirthDay4(new java.util.Date());
        p2.setBirthDay5(new Timestamp(System.currentTimeMillis()));
        p2.setBirthDay6(new java.util.Date());

        p2.setBirthDay7(LocalDateTime.now());
//        p2.setBirthDay5(DateUtil.parseLocalDateTime("1982-12-05 12:10:21"));

        ObjectMapper mapper = new ObjectMapper();
        //将Java对象进行序列化为JSON格式对象
        String jsonString = mapper.writeValueAsString(p2);
        System.out.println("序列化::Java对象-->JSON格式对象: " + jsonString);

        //将JSON格式对象反序列化为Java对象
        jsonString = "{\"age\":\"45\",\"name\":\"张三\"," +
                "\"birthDay\":\"1987-04-05 12:50:12\"," +
                "\"birthDay1\":[1988,4,5,12,50,12]," +
                "\"birthDay2\":\"1989-04-05\"," +
                "\"birthDay3\":1640830189656," +
                "\"birthDay4\":\"1989-04-05\"," +
                "\"birthDay5\":\"1989-04-05\"," +
                "\"birthDay6\":\"1989-04-05\"," +
                "\"birthDay7\":\"1989-04-05 12:50:12\"}";
        Person curry = mapper.readValue(jsonString, Person.class);
        //Exception in thread "main" java.lang.NoSuchFieldError: ACCEPT_CASE_INSENSITIVE_VALUES  报错
        System.out.println("反序列化::JSON格式对象-->Java对象:  " + curry);

//        ObjectMapper mapper1 = new ObjectMapper();
////序列化的时候序列对象的所有属性
//        mapper1.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//        mapper1.registerModule(new JavaTimeModule());
//        //将JSON字符串反序列化为java对象
//        Person curry = mapper1.readValue(jsonString, Person.class);
//        //Exception in thread "main" java.lang.NoSuchFieldError: ACCEPT_CASE_INSENSITIVE_VALUES  报错
//        System.out.println("将JSON字符串反序列化为java对象:" + curry);
    }
}
